package com.litchi.fragment;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindCallback;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.litchi.R;
import com.litchi.activity.MainActivity;
import com.litchi.activity.SearchActivity;
import com.litchi.bean.Brands;
import com.litchi.utils.Constants;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class BrandFragment extends Fragment implements OnClickListener {
	private View layout;
	private ListView mListView;
	private List<Brands> mList;
	private Context mContext;
	private View errorView;
	private TextView reloadTextView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (layout != null) {
			// 防止多次new出片段对象，造成图片错乱问题
			return layout;
		}
		mContext = getActivity();
		layout = inflater.inflate(R.layout.fragment_brand, container, false);
		layout.findViewById(R.id.img_category_search_code).setOnClickListener(this);
		layout.findViewById(R.id.layout_category_search).setOnClickListener(this);
		errorView=(View)layout.findViewById(R.id.error);
		reloadTextView=(TextView)layout.findViewById(R.id.reload);
		reloadTextView.setOnClickListener(this);
		initData();
		 
		return layout;
	}

	public void initData() {
	
		if (mList==null) {
			mList = new ArrayList<Brands>();

		}

		BmobQuery<Brands> query = new BmobQuery<Brands>("Brands");
		query.findObjects(mContext, new FindCallback() {

			@Override
			public void onFailure(int arg0, String arg1) {
				// TODO Auto-generated method stub
				Log.d(Constants.LOG, arg1.toString());
				layout.findViewById(R.id.progressBar).setVisibility(View.GONE);
				errorView.setVisibility(View.VISIBLE);
			}

			@Override
			public void onSuccess(JSONArray arg0) {
				// TODO Auto-generated method stub
				errorView.setVisibility(View.GONE);
				layout.findViewById(R.id.progressBar).setVisibility(View.GONE);
				Log.d(Constants.LOG, arg0.toString());
				Type type = new TypeToken<List<Brands>>() {
				}.getType();
				mList = new Gson().fromJson(arg0.toString(), type);

				Log.d(Constants.LOG, "" + mList.size());
				initView();
			}

		});
		
	}

	public void initView() {
		mListView = (ListView) layout.findViewById(R.id.brand_list);
		mListView.setVisibility(View.VISIBLE);
		Log.d(Constants.LOG + "initView", "" + mList.size());

		com.litchi.adapter.BrandAdapter mBrandAdapter = new com.litchi.adapter.BrandAdapter(mContext, mList);
		mListView.setAdapter(mBrandAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				  
				
			}

		});
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		// 将layout从父组件中移除
		ViewGroup parent = (ViewGroup) layout.getParent();
		parent.removeView(layout);
	}

	private void gotoSearch() {
		Intent intent = new Intent(getActivity(), SearchActivity.class);
		startActivity(intent);
		// activity开启无动画
		getActivity().overridePendingTransition(0, 0);
	}

	// 点击事件
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_category_search_code: // 二维码扫描
			((MainActivity) getActivity()).scanQRCode();
			break;
		case R.id.layout_category_search:
			gotoSearch();
			break;
		case R.id.reload:
			initData();
			break;
		default:
			break;
		}
	}
}
